﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CommunitySystem
{
    public partial class ChooseCandidatorWindow : ChildWindow
    {
        private readonly JinShengService.JinShen_MainInfo _MainInfo = null;
        private JinShengService.JinShen_ChooseCandidator_Main _ChooseMain = null;
        private readonly JinShengService.JinShengServiceClient _client = new JinShengService.JinShengServiceClient();
        private JinShengService.JinShen_Democraticevaluation_Main ValuationMain = null;
        public ChooseCandidatorWindow(JinShengService.JinShen_MainInfo mainInfo)
        {
            InitializeComponent(); 
            _MainInfo = mainInfo;
            _client.GetDemocraEvalutionCompleted += (sender, e) =>
            {
                ValuationMain = e.Result.LastOrDefault();
                Init();
                //TextBox_TextChanged(null, null);
            };
            _client.GetDemocraEvalutionAsync("MainID=" + _MainInfo.ID);

            _client.SaveJinShenMainInfoCompleted += (sender, e) =>
            {
                if (e.Error == null)
                {
                    if (_MainInfo.Status != CommunitySystem.PageView.JinShenConst.STATUS_Solution_Retire)
                        _client.SaveJinShen_ChooseCandidator_MainAsync(_ChooseMain);
                    else
                    {
                        SilverlightMessageBoxLibrary.Message.InfoMessage("保存成功");
                        this.DialogResult = true;
                    }
                }
            };
            _client.SaveJinShen_ChooseCandidator_MainCompleted += (sender, e) =>
            {
                if (e.Error == null && e.Result == true)
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("保存成功");
                    this.DialogResult = true;
                }
            };

        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (Validate())
            {

                _MainInfo.Status = CommunitySystem.PageView.JinShenConst.STATUS_Candidate_Inspect;
                if(_ChooseMain.Details.Count<= 0)
                    _MainInfo.Status = CommunitySystem.PageView.JinShenConst.STATUS_Solution_Retire;
                _client.SaveJinShenMainInfoAsync(_MainInfo);
            }
            //this.DialogResult = true;
        }

        private bool Validate()
        {
            if (_ChooseMain.MeetingDate == null)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("会议时间不能为空!");
                return false;
            }
            if (_ChooseMain.FilterRate < 0)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("不通过率不能小于0!");
                _ChooseMain.FilterRate = new decimal(33.33);
                return false;
            }
            return true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void TextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
        	// 在此处添加事件处理程序实现。
            try
            {
                decimal rate = decimal.Parse(tbRate.Text);
                LoadCadidator(rate);
            }
            catch (Exception ex)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage(ex.Message);
            }
        }

        private void LoadCadidator(decimal rate)
        {
            spDetails.Children.Clear();

            //_ChooseMain = new JinShengService.JinShen_ChooseCandidator_Main() { CreateDate = DateTime.Now, CreateUser = EHR.Common.Session.LoginUser.LoginName, MainID = _MainInfo.ID };
            if (_ChooseMain.Details == null)
                _ChooseMain.Details = new System.Collections.ObjectModel.ObservableCollection<JinShengService.JinShen_ChooseCandidator_Detail>();
            _ChooseMain.Details.Clear();
            //dpMeetingDate.DataContext = _ChooseMain;
            foreach (var d in ValuationMain.Details)
            {
                if (d.UnAgreeRate < rate)
                {

                    var detail = new CommunitySystem.JinShengService.JinShen_ChooseCandidator_Detail() { EmpID = d.EmpID, Name = d.EmpName };
                    _ChooseMain.Details.Add(detail);
                    StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, Margin = new Thickness(5, 3, 0, 2) };
                    TextBlock tb = new TextBlock() { Text = d.EmpName, Margin = new Thickness(0, 0, 3, 0), Width = 80 };
                    sp.Children.Add(tb);
                    Image img = new Image() { DataContext = detail, Tag = sp };
                    img.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("/CommunitySystem;component/Images/ViewDesign/delete.png", UriKind.RelativeOrAbsolute));
                    img.MouseLeftButtonDown += new MouseButtonEventHandler(img_MouseLeftButtonDown);
                    sp.Children.Add(img);
                    spDetails.Children.Add(sp);
                }
            }

        }

        void Init()
        {
            spDetails.Children.Clear();
            decimal rate = new decimal(33.33d);
            if (_MainInfo.Status == CommunitySystem.PageView.JinShenConst.STATUS_Candidate_Approved)//新增
            {
                _ChooseMain = new JinShengService.JinShen_ChooseCandidator_Main() { CreateDate = DateTime.Now, CreateUser = EHR.Common.Session.LoginUser.LoginName, MainID = _MainInfo.ID, FilterRate = new decimal(33.33) };
                if (_ChooseMain.Details == null)
                    _ChooseMain.Details = new System.Collections.ObjectModel.ObservableCollection<JinShengService.JinShen_ChooseCandidator_Detail>();
                _ChooseMain.Details.Clear();
                //dpMeetingDate.DataContext = _ChooseMain;
                this.DataContext = _ChooseMain;
                foreach (var d in ValuationMain.Details)
                {
                    if (d.UnAgreeRate < rate)
                    {
                        var detail = new CommunitySystem.JinShengService.JinShen_ChooseCandidator_Detail() { EmpID = d.EmpID, Name = d.EmpName };
                        _ChooseMain.Details.Add(detail);
                        StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, Margin = new Thickness(5, 3, 0, 2) };
                        TextBlock tb = new TextBlock() { Text = d.EmpName, Margin = new Thickness(0, 0, 3, 0), Width = 80 };
                        sp.Children.Add(tb);
                        Image img = new Image() { DataContext = detail, Tag = sp };
                        img.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("/CommunitySystem;component/Images/ViewDesign/delete.png", UriKind.RelativeOrAbsolute));
                        img.MouseLeftButtonDown += new MouseButtonEventHandler(img_MouseLeftButtonDown);
                        sp.Children.Add(img);
                        spDetails.Children.Add(sp);
                    }
                }
            }
            else if (_MainInfo.Status == CommunitySystem.PageView.JinShenConst.STATUS_Candidate_Inspect)//编辑
            {
                _client.GetJinShen_ChooseCandidator_MainCompleted += (sender, e) =>
                {
                    _ChooseMain = e.Result.LastOrDefault();
                    //dpMeetingDate.DataContext = _ChooseMain;
                    foreach (var d in _ChooseMain.Details)
                    {
                        StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, Margin = new Thickness(5, 3, 0, 2) };
                        TextBlock tb = new TextBlock() { Text = d.Name, Margin = new Thickness(0, 0, 3, 0), Width = 80 };
                        sp.Children.Add(tb);
                        Image img = new Image() { DataContext = d, Tag = sp };
                        img.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("/CommunitySystem;component/Images/ViewDesign/delete.png", UriKind.RelativeOrAbsolute));
                        img.MouseLeftButtonDown += new MouseButtonEventHandler(img_MouseLeftButtonDown);
                        sp.Children.Add(img);
                        spDetails.Children.Add(sp);
                        //tbRate.DataContext = _ChooseMain;
                    }
                    this.DataContext = _ChooseMain;
                };
                //tbRate.DataContext = _ChooseMain;
                
                _client.GetJinShen_ChooseCandidator_MainAsync("MainID=" + _MainInfo.ID);
                
            }
        }

        void img_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Image img = sender as Image;
            var detail = img.DataContext as JinShengService.JinShen_ChooseCandidator_Detail;
            StackPanel sp = img.Tag as StackPanel;
            if (detail != null)
            {
                _ChooseMain.Details.Remove(detail);
                spDetails.Children.Remove(sp);
            }
        }
    }
}

